package com.voip.sipphone;

import javax.sip.header.AllowHeader;
import javax.sip.header.HeaderFactory;
import javax.sip.header.UserAgentHeader;
import javax.sip.message.Request;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by RXJ on 2018/1/3.
 */
public class SipPhone {
    /**
     * username
     */
    public static String username="816";

    public static  String password="123456";
    /**
     * 服务器侦听IP地址
     */
    public static String ipAddr = "192.168.1.218";
    /**
     * 服务器侦听端口
     */
    public static int ipAddr_port = 5060;

    /**
     * branch
     */
    public static String branch ="z9hG4bK-d87543-ec03ee3e022acf28-1--d87543-";
    /**
     * tag
     */
    public static String tag = "0c0e9b77";
    /**
     * 设置 allowHeader 授予 所有方法权限
     * @param request
     * @param headerFactory
     * @return
     * @throws ParseException
     */
    public static Request setAllowHeader_REGISTER(Request request,HeaderFactory headerFactory) throws ParseException {
        AllowHeader allowHeader1 = headerFactory.createAllowHeader(Request.REGISTER);
        AllowHeader allowHeader2 = headerFactory.createAllowHeader(Request.ACK);
        AllowHeader allowHeader3 = headerFactory.createAllowHeader(Request.CANCEL);
        AllowHeader allowHeader4 = headerFactory.createAllowHeader(Request.OPTIONS);
        AllowHeader allowHeader5 = headerFactory.createAllowHeader(Request.BYE);
        AllowHeader allowHeader6 = headerFactory.createAllowHeader(Request.REFER);
        AllowHeader allowHeader7 = headerFactory.createAllowHeader(Request.NOTIFY);
        AllowHeader allowHeader8 = headerFactory.createAllowHeader(Request.MESSAGE);
        AllowHeader allowHeader9 = headerFactory.createAllowHeader(Request.SUBSCRIBE);
        AllowHeader allowHeader10 = headerFactory.createAllowHeader(Request.INFO);
        request.addHeader(allowHeader1);
        request.addHeader(allowHeader2);
        request.addHeader(allowHeader3);
        request.addHeader(allowHeader4);
        request.addHeader(allowHeader5);
        request.addHeader(allowHeader6);
        request.addHeader(allowHeader7);
        request.addHeader(allowHeader8);
        request.addHeader(allowHeader9);
        request.addHeader(allowHeader10);
        return request;
    }


    /**
     * 设置 allowHeader 授予 INVITE
     * @param request
     * @param headerFactory
     * @return
     * @throws ParseException
     */
    public static Request setAllowHeader_INVITE(Request request,HeaderFactory headerFactory) throws ParseException {
        AllowHeader allowHeader1 = headerFactory.createAllowHeader(Request.INVITE);
        AllowHeader allowHeader2 = headerFactory.createAllowHeader(Request.ACK);
        AllowHeader allowHeader3 = headerFactory.createAllowHeader(Request.CANCEL);
        AllowHeader allowHeader4 = headerFactory.createAllowHeader(Request.OPTIONS);
        AllowHeader allowHeader5 = headerFactory.createAllowHeader(Request.BYE);
        AllowHeader allowHeader6 = headerFactory.createAllowHeader(Request.REFER);
        AllowHeader allowHeader7 = headerFactory.createAllowHeader(Request.NOTIFY);
        AllowHeader allowHeader8 = headerFactory.createAllowHeader(Request.MESSAGE);
        AllowHeader allowHeader9 = headerFactory.createAllowHeader(Request.SUBSCRIBE);
        AllowHeader allowHeader10 = headerFactory.createAllowHeader(Request.INFO);
        request.addHeader(allowHeader1);
        request.addHeader(allowHeader2);
        request.addHeader(allowHeader3);
        request.addHeader(allowHeader4);
        request.addHeader(allowHeader5);
        request.addHeader(allowHeader6);
        request.addHeader(allowHeader7);
        request.addHeader(allowHeader8);
        request.addHeader(allowHeader9);
        request.addHeader(allowHeader10);
        return request;
    }

    /**
     * 第二步注册时 根据 第一次返回的验证信息计算 验证信息中的 response值
     * @param username 用户名
     * @param realm     realm
     * @param password  密码
     * @param method    方法:"REGISTER"
     * @param uri       请求地址 ： sip:192.168.1.218
     * @param nonce     请求认证码
     * @return response 计算后的值
     */
    public static String getResponse(
            String username,String realm,String password,String method,
            String uri,String nonce){
        String hex1 = MD5Util.EncoderByM5(username+":"+realm+":"+password);
        String hex2 =MD5Util.EncoderByM5(method+":"+uri);
        String result = MD5Util.EncoderByM5(hex1+":"+nonce+":"+hex2);
        return result;
    }

    /**
     * 设置 allowHeader 授予 所有方法权限
     * @param request
     * @param headerFactory
     * @return
     * @throws ParseException
     */
    public static Request setAllowHeader_SUBSCRIBE(Request request,HeaderFactory headerFactory) throws ParseException {
        AllowHeader allowHeader1 = headerFactory.createAllowHeader(Request.INVITE);
        AllowHeader allowHeader2 = headerFactory.createAllowHeader(Request.ACK);
        AllowHeader allowHeader3 = headerFactory.createAllowHeader(Request.CANCEL);
        AllowHeader allowHeader4 = headerFactory.createAllowHeader(Request.OPTIONS);
        AllowHeader allowHeader5 = headerFactory.createAllowHeader(Request.BYE);
        AllowHeader allowHeader6 = headerFactory.createAllowHeader(Request.REFER);
        AllowHeader allowHeader7 = headerFactory.createAllowHeader(Request.NOTIFY);
        AllowHeader allowHeader8 = headerFactory.createAllowHeader(Request.MESSAGE);
        AllowHeader allowHeader9 = headerFactory.createAllowHeader(Request.SUBSCRIBE);
        AllowHeader allowHeader10 = headerFactory.createAllowHeader(Request.INFO);
        request.addHeader(allowHeader1);
        request.addHeader(allowHeader2);
        request.addHeader(allowHeader3);
        request.addHeader(allowHeader4);
        request.addHeader(allowHeader5);
        request.addHeader(allowHeader6);
        request.addHeader(allowHeader7);
        request.addHeader(allowHeader8);
        request.addHeader(allowHeader9);
        request.addHeader(allowHeader10);
        return request;
    }

    /**
     * set userAgent
     * @return
     */
    public static List<String> setUserAgentHeader(){
        List<String> userAgentList = new ArrayList<>();
        userAgentList.add(" eyeBeam");
        userAgentList.add(" release");
        userAgentList.add(" 1011d");
        userAgentList.add(" stamp");
        userAgentList.add(" 40820");
        return userAgentList;
    }

    /**
     *发送信息请求
     * @param to
     * @param message

    public void sendMessage(String to, String message)
    throws ParseException, InvalidArgumentException, SipException {
    //from 创建本地请求用户名地址
    SipURI from = addressFactory.createSipURI(
    getUsername(), getHost() + ":" + getPort());
    Address fromNameAddress = addressFactory.createAddress(from);

    fromNameAddress.setDisplayName(getUsername());
    //创建 本地请求头
    FromHeader fromHeader = headerFactory.createFromHeader(
    fromNameAddress, "textclientv1.0");
    //to 请求用户名和地址
    String username = to.substring(to.indexOf(":") + 1, to.indexOf("@"));
    String address = to.substring(to.indexOf("@") + 1);
    SipURI toAddress = addressFactory.createSipURI(username, address);
    Address toNameAddress = addressFactory.createAddress(toAddress);
    toNameAddress.setDisplayName(username);
    //to 请求头
    ToHeader toHeader = headerFactory.createToHeader(toNameAddress, null);

    SipURI requestURI = addressFactory.createSipURI(username, address);
    requestURI.setTransportParam("udp");

    ArrayList<ViaHeader> viaHeaders = new ArrayList<ViaHeader>();

    //创建 via
    ViaHeader viaHeader = headerFactory.createViaHeader(
    getHost(), getPort(), "udp", null);
    viaHeaders.add(viaHeader);

    //创建 CALL_ID
    CallIdHeader callIdHeader = sipProvider.getNewCallId();
    //创建 cSeq  Request 为调用方法
    CSeqHeader cSeqHeader = headerFactory.createCSeqHeader(1L, Request.MESSAGE);
    //创建  Max_forwards 默认：70
    MaxForwardsHeader maxForwards = headerFactory.createMaxForwardsHeader(70);

    //创建请求对象
    Request request = messageFactory.createRequest(requestURI,
    Request.MESSAGE, callIdHeader, cSeqHeader, fromHeader, toHeader, viaHeaders, maxForwards);
    //创建 Contact对象  Contact URI
    SipURI contactURI = addressFactory.createSipURI(getUsername(), getHost());
    contactURI.setPort(getPort());

    Address contactAddress = addressFactory.createAddress(contactURI);
    contactAddress.setDisplayName(getUsername());
    ContactHeader contactHeader = headerFactory.createContactHeader(contactAddress);
    request.addHeader(contactHeader);
    ContentTypeHeader contentTypeHeader = headerFactory.createContentTypeHeader("text", "plain");
    request.setContent(message, contentTypeHeader);

    //发送请求
    //ClientTransaction inviteTid =sipProvider.getNewClientTransaction(request);
    //inviteTid.sendRequest();
    //发送请求
    // Create the client transaction.

    listener.subscribeTid = sipProvider.getNewClientTransaction(request);
    // send the request out.
    listener.subscribeTid.sendRequest();
    }
     */
}
